//
//  RankingChildViewController.m
//  rrqs
//
//  Created by 朱耀春 on 2019/4/30.
//

#import "RankingChildViewController.h"
#import "RankingTableViewCell.h"
#import "RankingModel.h"


@interface RankingChildViewController ()
<UITableViewDelegate,
UITableViewDataSource>
{
    NSDictionary * dataDict;
}

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@property (nonatomic, copy)  NSMutableArray * DateArr;


@end

@implementation RankingChildViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    
    //添加
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdatStatus:) name:@"UpdatStatus" object:nil];
    //发送
//    //刷新、加载
//    _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self queryData];
//    }];
//
//    _mainTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        [self queryData];
//    }];
    [self queryData];
}
-(void)UpdatStatus:(NSNotification *)not{
    [self queryData];
}
//释放通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    if (self.DateArr.count > 0 ) {
        return self.DateArr.count;
    }
    return 0;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.DateArr.count > 0 ) {
        return 2;
    }
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 ) {
        return 0.01;
    }
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
   
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
#pragma mark 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RankingTableViewCell * cell = (RankingTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"RankingTableViewCell"];
    cell.type = [_type integerValue];
    if (indexPath.section == 0 ) {
        if (dataDict.count > 0) {
            cell.me_data = dataDict;
        }
    }else{
        if (self.DateArr.count > 0 ) {
            cell.rankingL.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
            cell.other_data = self.DateArr[indexPath.row];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.separatorInset = UIEdgeInsetsMake(0, 30, 0, 20);
    return cell;
}
#pragma mark 点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)queryData {
    NSDictionary* arg = @{@"ince":@"get_getorder_tanking",
                          @"status":[UserInfo getRankingStatus],
                          @"type":_type,
                      @"emp_id":self.Identity.userInfo.userID};
    NSLog(@"参数： %@",arg);
     __weak typeof(self) weakSelf = self;
    [DBNet PostURL:kHost parameters:arg succeed:^(NSDictionary *data) {
        [WMHelper outPutJsonString:data];
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            [WMHelper outPutJsonString:data];
            dataDict = data;
            
            switch ([_type integerValue]) {
                case 1:
                {
                    weakSelf.DateArr = data[@"other_danliang"];
                }
                    break;
                case 2:
                {
                    weakSelf.DateArr = data[@"other_zhunshi"];
                }
                    break;
                case 3:
                {
                    weakSelf.DateArr = data[@"other_shichang"];
                }
                    break;
                case 4:
                {
                    weakSelf.DateArr = data[@"other_licheng"];
                }
                    break;
                    
                default:
                    break;
            }
            [weakSelf.mainTableView reloadData];
            [weakSelf.mainTableView.mj_header endRefreshing];
            
        }else{
            [self alertHUD:[data objectForKey:@"fail"]];
        }
        
    } failure:^(NSError *error) {
        [self alertHUD:@"网络异常"];
    }];
}






@end
